/**
 * 初始化
 */
$(function(){
	getUserInfo();
	getUserAccountInfo();
	
});

/**
 * 获取当前用户信息
 */
function getUserInfo(){
	var currentUser;
	$.ajax({
		type:"get",
		url:basePath + "/user/detail",
		/*cache:false, 
	    ifModified :true ,*/
		success:function(data){
			if(data){
				currentUser = data.obj;
				loadData(currentUser);
			}
		},
		async:false,
	});
	return currentUser;
}

/**
 * 填充数据
 */
function loadData(data){
//	var data = getUserInfo();
	$("#userInfoFrom div.infotext span.userId").html(data.id);
	$("#userInfoFrom div.infotext span.userName").html(data.loginName);
	$("#userInfoFrom div.infotext span.nikeName").html(data.nickName);
	
	$("#userInfoFrom div.infotext span.tel").html(data.phone);
	
	if(data.gender == 1){
		$("#userInfoFrom div.infotext span.gender").html("女");
	}else{
		$("#userInfoFrom div.infotext span.gender").html("男");
	}
	$("#userInfoFrom div.infotext input.nikeName").hide();
	$("#userInfoFrom div.infotext input.tel").hide();
	$("#userInfoFrom div.infotext span.nikeName").show();
	$("#userInfoFrom div.infotext span.tel").show();
}

/**
 * 修改用户资料
 */
function changeUserInfo(){
	$("#userInfoFrom div.infotext span.nikeName").hide();
	$("#userInfoFrom div.infotext span.tel").hide();
	
	$("#userInfoFrom div.infotext input.nikeName").show();
	$("#userInfoFrom div.infotext input.nikeName").val($("#userInfoFrom div.infotext span.nikeName").html());
	$("#userInfoFrom div.infotext input.tel").show();
	$("#userInfoFrom div.infotext input.tel").val($("#userInfoFrom div.infotext span.tel").html());
	
	$(".commit").show();
}

/**
 * 保存修改
 */
function savechange(){
	var params = {};
	params= getUserInfo();
	params.nickName = $("#userInfoFrom div.infotext input.nikeName").val();
	params.phone = $("#userInfoFrom div.infotext input.tel").val();
	params.createTime = (new Date(params.createTime)).Format("yyyy-MM-dd");
	params.lastLoginTime = (new Date(params.lastLoginTime)).Format("yyyy-MM-dd");
	params.lastModifiedTime = (new Date(params.lastModifiedTime)).Format("yyyy-MM-dd");
	console.log(params);
	
	$.ajax({
		type : "post",
		url : basePath + "/user/update",
		data:params,
		async : true,
		success : function(data) {
			if (data.status == "200") {
				getUserInfo();
				/*$("#userInfoFrom div.infotext span.nikeName").html(params.nickName);
				
				$("#userInfoFrom div.infotext span.tel").html(params.phone);*/
				$(".commit").hide();
			}else{
				alert("保存失败");
			}
		}
	});
}

/**
 * 获取当前登录用户下的所有账户信息
 */
function getUserAccountInfo() {
	$.ajax({
		type : "get",
		url : basePath + "/user/account",
		async : true,
		success : function(data) {
			if (data) {
				$("#userInfoFrom div.infotext span.countMany").html(data.obj.countMany);
			}
		}
	});
}


/**
 * 格式化时间
 */
Date.prototype.Format = function (fmt) { //author: tony  
    var o = {  
        "M+": this.getMonth() + 1, //月份  
        "d+": this.getDate(), //日  
        "h+": this.getHours(), //小时  
        "m+": this.getMinutes(), //分  
        "s+": this.getSeconds(), //秒  
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度  
        "S": this.getMilliseconds() //毫秒  
    };  
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));  
    for (var k in o)  
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));  
    return fmt;  
};



